/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.solr.schema;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pingan.haofang.searchcloud.solr.constants.SolrConstant;



/**
 * @author huhailiang937
 * @date 2017年5月4日下午1:07:47
 * @since 1.0.0
 */
public class GeneraterFileWriteSupport {

    protected static Logger LOG = LoggerFactory.getLogger(GeneraterFileWriteSupport.class);
    
    protected static final String FILE_SEPARATOR = System.getProperty("file.separator");
    
    
    public static String productDesFileAbsolutePath(String targetPath,String javaPackage,String fileName){
        boolean targetPathEndWith =targetPath.endsWith(FILE_SEPARATOR); 
        String javaPackagePath = javaPackage.replace(".", FILE_SEPARATOR);
        
        StringBuffer desFileAbsolutePath = new StringBuffer();
        desFileAbsolutePath.append(targetPath);
        desFileAbsolutePath.append(targetPathEndWith?"":FILE_SEPARATOR);
        desFileAbsolutePath.append(javaPackagePath);
        if(StringUtils.isNotEmpty(fileName)){
            desFileAbsolutePath.append(FILE_SEPARATOR);
            desFileAbsolutePath.append(fileName);
        }
        return desFileAbsolutePath.toString();
    }
    
    
    public static String productAbsolutePath(String targetPath,String javaPath ,String fileName){
        boolean targetPathEndWith =targetPath.endsWith(FILE_SEPARATOR); 
        String javaPackagePath = javaPath.replace(".", FILE_SEPARATOR);
        
        StringBuffer desFileAbsolutePath = new StringBuffer();
        desFileAbsolutePath.append(targetPath);
        desFileAbsolutePath.append(targetPathEndWith?"":FILE_SEPARATOR);
        desFileAbsolutePath.append(javaPackagePath);
        if(StringUtils.isNotEmpty(fileName)){
            desFileAbsolutePath.append(FILE_SEPARATOR);
            desFileAbsolutePath.append(fileName);
        }
        return desFileAbsolutePath.toString();
    }
    
    
    public static void mkdir(String fileAbsolutePath){
        File file = new File(fileAbsolutePath);//创建多级目录文件  
        try {
            file.getParentFile().mkdirs();  
        } catch (Exception e) {
            LOG.error("mkdir has error", e);
            throw new RuntimeException("mkdir has error",e);
        }  finally{
            LOG.info(fileAbsolutePath + " --> generator sucess!");
        }
    }
    
    public static void writeGeneratorCoder(String coder,String fileAbsolutePath){
        File file = new File(fileAbsolutePath);//创建多级目录文件  
        BufferedWriter output = null;
        try {
            file.getParentFile().mkdirs();  
            file.createNewFile();
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file,false),SolrConstant.GLOBAL_CHAR_SET_ENCODING); 
            output = new BufferedWriter(write);  
            output.write(coder);  
        } catch (Exception e) {
            LOG.error("writeGeneratorCoder has error", e);
            throw new RuntimeException("writeGeneratorCoder has error",e);
        }  finally{
            if(null != output){
                try {
                    output.flush();
                    LOG.info(fileAbsolutePath + " --> generator sucess!");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }  
            }
        }
    }

}
